﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharepointSolutionInstaller.Utils.Configuration;

namespace SharepointSolutionInstaller.Views
{
    public partial class InstallerContainerControl : Form
    {

        #region Members

        public List<InstallerControl> contentControls;
        private InstallOptions options;
        public InstallOperation operation = InstallOperation.Install;
        private InstallerControl currentContentControl;
        private int currentContentControlIndex = 0;

        #endregion

        #region Constructors

        public InstallerContainerControl()
        {
            InitializeComponent();
            lkCodePlex.Links.Add(0, lkCodePlex.Text.Length, "http://splistsecurity.codeplex.com/");
            lkIptech.Links.Add(0, lkIptech.Text.Length, "http://www.iptech-group.com/");
            InitView();
            InitEvents();


        }

        #endregion

        #region Private methods

        void InitView()
        {
            contentControls = new List<InstallerControl>();
            contentControls.Add(new WelcomeView());
            contentControls.Add(new CheckPrerequisiteView());
            this.options = new InstallOptions();
        }

        void InitEvents()
        {
            btnNext.Click += new EventHandler(btnNext_Click);
            btnPrevious.Click += new EventHandler(btnPrevious_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);
            this.Load += new EventHandler(InstallerForm_Load);
        }

        private void ReplaceContentControl(int index)
        {
            //Close current view
            if (currentContentControl != null)
                currentContentControl.Close(options);
            //EnableDisable buttons
            btnNext.Enabled = index != contentControls.Count - 1;
            btnPrevious.Enabled = index != 0;
            //Load new view
            InstallerControl newContentControl = contentControls[index];
            newContentControl.Dock = DockStyle.Fill;
            //Set Title
            titleLabel.Text = newContentControl.Title;
            subTitleLabel.Text = newContentControl.SubTitle;
            //Display tne new view
            pnlFill.Controls.Clear();
            pnlFill.Controls.Add(newContentControl);
            newContentControl.Open(options);
            //Set the current view
            currentContentControl = newContentControl;
        }

        #endregion

        #region Events

        private void InstallerForm_Load(object sender, EventArgs e)
        {
            ReplaceContentControl(0);
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            currentContentControl.RequestCancel();
        }

        void btnPrevious_Click(object sender, EventArgs e)
        {
            currentContentControlIndex--;
            ReplaceContentControl(currentContentControlIndex);
        }

        void btnNext_Click(object sender, EventArgs e)
        {
            currentContentControlIndex++;
            ReplaceContentControl(currentContentControlIndex);
        }

        #endregion

        public void SetTitle(string title)
        {
            titleLabel.Text = title;
        }

        public void SetSubTitle(string title)
        {
            subTitleLabel.Text = title;
        }

        private void Link_Cliked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
        }
    }
}
